package CollectionDemo.ShowHand.pojo;

import sun.security.krb5.internal.crypto.Des;

import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

/*
赌局
 */
public class Poke {
    //用来存牌
    private LinkedList<Desk> linkedList = new LinkedList<Desk>();
    private String[] follows = {"菜花", "红心", "黑桃", "方块"};

    //庄家
    private Player computer = new Player("庄家");
    //玩家
    private Player player;

    public Poke(Player player) {
        this.player = player;
    }

    //初始化牌组
    public void InitPoke() {
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < follows.length; j++) {
                Desk desk = new Desk(follows[j],i);
                linkedList.add(desk);
            }
        }
        Desk desk1 = new Desk("小王",14);
        linkedList.add(desk1);
        Desk desk2 = new Desk("大王",15);
        linkedList.add(desk2);

    }

    //洗牌
    public void Shuffle() {
        Collections.shuffle(linkedList);
    }

    //给玩家一张牌
    public void givePlayerHand() {
        Shuffle();
        player.getHand(linkedList.get(0));
        linkedList.remove(0);
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        if (i == 1) {
            givePlayerHand();
        } else {
            return;
        }
    }

    //电脑要牌
    public void giveComputerHand() {
        int sum = 0;
        for (int i = 0; i < computer.getHands().size(); i++) {
            sum += computer.getHands().get(i).getValue();
        }
        while (sum <= 16) {
            Shuffle();
            computer.getHand(linkedList.get(0));
            sum += linkedList.get(0).getValue();
            linkedList.remove(0);
        }
    }

    //判断胜负
    public void WhoWin() {
        int computerSorce = 0;
        int playerSorce = 0;
        for (int i = 0; i < computer.getHands().size(); i++) {
            computerSorce += computer.getHands().get(i).getValue();
        }
        for (int j = 0; j < player.getHands().size(); j++) {
            playerSorce += player.getHands().get(j).getValue();
        }
        if (computerSorce > playerSorce) {
            System.out.println("庄家获胜");
        } else if (computerSorce < playerSorce) {
            System.out.println("玩家获胜");
        } else {
            System.out.println("双方平手");
        }
    }

    public static void main(String[] args) {

        Player player = new Player("十七");
        Poke poke = new Poke(player);
        poke.InitPoke();
        poke.giveComputerHand();
        poke.givePlayerHand();
        poke.WhoWin();


    }


}
